/*
Input:  newdata/estimation_prep_ltw18_voter [prepared address-level panel]
		newdata/wahllokal_change 		   [prepared polling place level panel]

Output:
	- Figures 4, 5, D.1, D.2, D.4
	
Tasks: Descriptives at the VOTER-ADDRESS level and POLLING PLACE level
	
*/	


		
********************************************************************************
						// Voter-address level stats //
********************************************************************************

* PULL: Address-level data
	use "$newdata/estimation_prep_ltw18_voter.dta", clear
	
	* PLOT: FIGURE D4. Frequency of Polling Place Reassignments per Residential Address
frame copy default tmp, replace
frame tmp {
	cap drop tmp*
	
	bys voter_id: egen tmp_nbr_reassigned = total(treat_simple) // tot nbr of reassignments
	
	tab tmp_nbr_reassigned   // tab #reassignments per address
	
	// Among those reassigned 1+, how often is this a return to an old PP?
	bys voter_id: egen tmp_totnew= total(changed_wl) // tot nbr of change to NEW PP
	tab tmp_totnew if tmp_nbr_reassigned==1					
	tab tmp_totnew if tmp_nbr_reassigned==2 				
	tab tmp_totnew if tmp_nbr_reassigned==3 					
	tab tmp_totnew if tmp_nbr_reassigned==4 					
	
	// For those reassigned TWICE, how long is the period b/w first and second reassignment?
	gen tmp_neg_wahl_id = -wahl_id
	bys voter_id (treat_simple tmp_neg_wahl_id): gen tmp_first = wahl_id[_N] if tmp_nbr_reassigned>1
	bys voter_id (treat_simple tmp_neg_wahl_id): gen tmp_second = wahl_id[_N-1] if tmp_nbr_reassigned>1
	gen tmp_period = tmp_second-tmp_first
	
	tab tmp_period if tmp_nbr_reassigned==2 
	tab tmp_period if tmp_nbr_reassigned>1
	su  tmp_period if tmp_nbr_reassigned==2, det 
	su  tmp_period if tmp_nbr_reassigned>1, det 
	
	// keep one obs per voter
	bys voter_id: keep if _n ==1
	su tmp_nbr_reassigned, det
	local p50 = r(p50)
	local avg = r(mean)
	
	* PLOT	
	twoway 	(hist tmp_nbr_reassigned , 	percent fcol(%0) discrete gap(2) ) ///
			, scheme (plotplain) xline(`avg', lcol(maroon) lpat(solid)) ///
			xtitle("Number of reassignments per address between 2013 and 2020") aspect(.5) name(freq, replace)

	graph export "$figures/Figure_D4_voter_reassignment_freq.pdf", replace
}	


* Treatment share over time: Stacked bar plot by reason of reassignment
frame copy default tmp, replace
frame tmp {
	collapse (mean) treat_*, by(wahl_id)

	label define wahl_id 1 "State 2013", modify
	label define wahl_id 2 "Federal 2013", modify
	label define wahl_id 3 "Municipal 2014", modify
	label define wahl_id 4 "European 2014", modify
	label define wahl_id 5 "Federal 2017", modify
	label define wahl_id 6 "State 2018", modify
	label define wahl_id 7 "European 2019", modify
	label define wahl_id 8 "Municipal 2020", modify
	
	replace treat_no_consol = treat_no_consol*100
	replace treat_consol = treat_consol*100
	
	* PLOT: Figure 4. Share of reassigned addresses by reassigment reason
	graph bar treat_no_consol treat_consol, over(wahl_id, relabel(1 `" "State" "2013" "' 2 `" "Federal" "2013" "' ///
		3 `" "Municipal" "2014" "' 4 `" "European" "2014" "' 5 `" "Federal" "2017" "' ///
		6 `" "State" "2018" "' 7 `" "European" "2019" "' 8 `" "Municipal" "2020" "' ) gap(10))  ///
		stack title("") ytitle("% Reassigned home addresses", size(medsmall)) b1title("Election") ///
		bargap(5) bar(2, fcolor(white) lcol(black)) bar(1, fcolor(black%50) lcol(black)) aspect(.5)  ///
		legend(label(1 "due polling location recruitment") label(2 "due to precinct reconfiguration") ///
		col(1) pos(9) ring(0) title("Polling place reassignments", size(small) justification(left) bexpand))

	graph export "$figures/Figure_4_bar_share_treated2_HNR_over_time.pdf", replace	
}

	
 * Density: Walking Distance
	// walking distance, overall
	su street_dist,det 
	local p50=r(mean)
	tw (hist street_dist, fcol(%0) xaxis(1 2) xla(`p50' `p50', axis(2) notick format(%3.2f)) ) ///
		, xtitle(Walking distance to polling place (km)) xtitle("", axis(2))  xline(`p50' , lcol(maroon) lwidth(.3) lpat(solid)) ///
		 legend(off) name(dist, replace)  aspect(.7) xlab(#8)
	
	// CHANGE in walking distance conditional on reassignment
	su del_street_dist if treat_no_consol == 1,det
	su del_street_dist if treat_consol == 1,det
	su del_street_dist if treat_simple!=0,det
	local p50=r(mean)
	
	* PLOT: FIGURE 5. Density plots of walking distance and change in distance
	tw 		(hist del_street_dist if treat_no_consol == 1 , fcol(black%40) lcol(%0) xaxis(1 2) xla(`p50' `p50', axis(2) notick format(%3.2f)) ) ///
			(hist del_street_dist if treat_consol ==1 , fcol(%0))  ///
		, xtitle("Change in walking distance after reassignment (km)") ///
		xtitle("", axis(2))  ytitle("  ") xline(`p50' , lcol(maroon) lwidth(.3) lpat(solid) ) ///
		legend(order(1 "due to polling location" "recruitment" 2 "due to precinct" "reconfiguration") ring(0) pos(1) title("Polling place reassignment", size(small))) ///
		xlab(#8) name(chdist, replace)   aspect(.7)	
	
	graph combine dist chdist, ycommon imargin(zero)
	graph export "$figures/Figure_5_Density_distances_street.pdf", replace
	
	// Change in distance in absolute terms
	gen del_street_dist_abs = abs(del_street_dist)
	su del_street_dist_abs if treat_simple!=0,det
	
		
********************************************************************************
						// POLLING PLACE level stats //
********************************************************************************

	gdistinct wl_id				// 293 distinct WL venues
	bys wahl_id: distinct wl_id
	by wahl_id: distinct sb_old	// #unharmonized precincts
	
	
	// precincts per WL															
	di 618/211 //  LTW2018: 2.92891
	di 702/197 //  LTW2013: 3.56345
	di 755/221 //  KOW2020: 3.4162896
	di 702/196 //  KOW2014: 3.5816327
	di 618/204 //  EUW2019: 3.02941
	di 702/196 //  EUW2014: 3.58163
	di 617/220 //  BTW2017: 2.8045
	di 702/197 //  BTW2013: 3.563
	
	* PLOT: FIGURE D1. Types of Polling Venues
frame copy default tmp, replace
frame tmp {
	gduplicates drop wl_id, force
	gen obs = 1
	collapse (count) obs, by(wl_kat)
	gsort -obs
	gen order = _n
	replace order = 10 if wl_kat == "Other"
	
	su obs
	gen pct = 100*(obs/r(sum) )
	
	encode wl_kat, gen(tmp)
	replace tmp = tmp + 99 if wl_kat == "Other"
	labmask tmp, values(wl_kat)
	
	graph bar  pct, over(wl_kat, sort(order) ) intensity(0) horizontal scheme(plotplain) ///
	ytitle("Percent") aspect(.4) ytick(0(10)80, grid) 
	graph export "$figures/Figure_D1_WL_categories.pdf", replace

}


* PLOT: FIGURE D2. Activity Status of Polling Venues between 2009 and 2020
frame copy default tmp, replace
frame tmp {
	
* PULL: Polling place panel	
use "$newdata/wahllokal_change",clear
	
	distinct wl_id if wahl_id>0 & wl_active==1 // 293
	
	keep wl_id wl_typ wahl_id wl_active
	
	expand 2 if wahl_id == 0, gen(new) // gen EuE-09 (assume same WL)
	replace wahl_id = -1 if new==1
	drop new
		
	cap drop tmp*
	bys wl_id: egen tmp_cnt = total(wl_active) // total periods active
	
	gen tmp_wahl_id = (-1)*wahl_id
	bys wl_id (wl_active tmp_wahl_id): gen tmp_first = wahl_id[_N] // wahl_id where first active
	replace tmp_first = (-1)*tmp_first // reverse for sorting
	
	// sort precincts by never-treated > most frequently treated
	egen tmp_id = group(tmp_first tmp_cnt wl_id)
	
	
	// Plot 
	heatplot wl_active i.tmp_id i.wahl_id, level(2) colors(gs15%30 black) ///
	ylab(0(10)293, labsize(vsmall) nogrid) ysc(r(1,293)) xlab(1 `" "European" "2009" "' 2 `" "Federal" "2009" "' 3 `" "State" "2013" "' 4 `" "Federal" "2013" "' ///
		5 `" "Municipal" "2014" "' 6 `" "European" "2014" "' 7 `" "Federal" "2017" "' ///
		8 `" "State" "2018" "' 9 `" "European" "2019" "' 10 `" "Municipal" "2020" "',labsize(2.7pt)  nogrid) ///
	ytitle("Polling venue ID") xtitle("Election") scheme(plotplain) discrete  statistic(asis)  ///
	ramp(lab(0 1) length(50) space(5))  aspect(0.55) xsc(titlegap(large)) 
	
	gr_edit .plotregion1.graph1.xaxis1.edit_tick 1 1 `" "European" "2009" "', tickset(major) editstyle(tickstyle(textstyle(color(maroon))) )
	gr_edit .plotregion1.graph1.xaxis1.edit_tick 2 2 `" "Federal" "2009" "', tickset(major) editstyle(tickstyle(textstyle(color(maroon))) )
	gr_edit .plotregion1.graph2.subtitle.text = {}
	gr_edit .plotregion1.graph2.subtitle.text.Arrpush Polling venue active
	gr_edit .plotregion1.graph2.subtitle.style.editstyle size(small) editcopy
	
	graph export "$figures/Figure_D2_heatmap_wl_active_2009_20.pdf", replace
	
}	

	